package com.mftour.moudles.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mftour.common.Constant;
import com.mftour.common.utils.DesUtil;
import com.mftour.common.utils.JsonUtil;
import com.mftour.moudles.entity.User;
import com.mftour.moudles.service.UserService;
import com.mftour.moudles.vo.AjaxResponse;
import com.mftour.moudles.vo.ShowPage;
import com.mftour.moudles.vo.search.SearchUserPagingVo;

/**
 * This class was generated by Bill Generator. This class corresponds to the
 * database table users 用户
 *
 * @mbggenerated 2015-07-25 06:07
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {
	@Autowired
	UserService userSer;

	@RequestMapping(value = "/list")
	public ModelAndView toUserList(SearchUserPagingVo searchUserPagingVo) {
		ModelAndView mv = new ModelAndView("system/userList");
		searchUserPagingVo.setPageSize(5);
		List<User> userList = userSer.selectListSelectivePaging(searchUserPagingVo);
		ShowPage page = new ShowPage(searchUserPagingVo, userList);
		mv.addObject("page", page);
		mv.addObject("searchUserPagingVo", searchUserPagingVo);
		return mv;
	}

	@RequestMapping(value = "/ajax/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(User user) {
		user.setUserId(null);
		user.setCreatedUser(loginUser.getUserId().toString());
		user.setPwd(DesUtil.encrypt(user.getLoginId() + user.getPwd()));
		userSer.insertSelective(user);
		if (user.getUserId() == null) {
			AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_ERROR, "添加用户出错！");
			return JsonUtil.writeValueAsString(ajaxResponse);
		}
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "添加用户成功！");
		return JsonUtil.writeValueAsString(ajaxResponse);
	}

	@RequestMapping(value = "/ajax/del", method = RequestMethod.POST)
	@ResponseBody
	public String del(Integer userId) {
		userSer.deleteByPrimaryKey(userId);
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "删除用户成功！");
		return JsonUtil.writeValueAsString(ajaxResponse);
	}

	@RequestMapping(value = "/ajax/delAll", method = RequestMethod.POST)
	@ResponseBody
	public String delAll(Integer[] userIds) {
		AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "请选择删除项！");
		if (userIds == null || userIds.length < 1) {
			return JsonUtil.writeValueAsString(ajaxResponse);
		}
		userSer.deleteByPKs(userIds);
		ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
		ajaxResponse.setMsg("删除用户成功!");
		return JsonUtil.writeValueAsString(ajaxResponse);
	}

	@RequestMapping(value = "/ajax/update", method = RequestMethod.POST)
	@ResponseBody
	public String update(User user) {
		user.setPwd(DesUtil.encrypt(user.getLoginId() + user.getPwd()));
		userSer.updateByPrimaryKeySelective(user);
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "修改用户成功！");
		return JsonUtil.writeValueAsString(ajaxResponse);
	}

	@RequestMapping(value = "/ajax/entity", method = RequestMethod.GET)
	@ResponseBody
	public String getEntity(Integer userId) {
		User user = userSer.selectByPrimaryKey(userId);
		AjaxResponse ajaxResponse = new AjaxResponse(Constant.RS_CODE_SUCCESS, "获取用户成功！");
		if (user != null) {
			user.setPwd(DesUtil.decrypt(user.getPwd()));
			int len = user.getLoginId().length();
			user.setPwd(user.getPwd().substring(len));
			ajaxResponse.setData(user);
		} else {
			ajaxResponse.setCode(Constant.RS_CODE_ERROR);
			ajaxResponse.setMsg("获取用户失败！");
		}
		return JsonUtil.writeValueAsString(ajaxResponse);
	}
}